﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Xianfen.Net.GetColor
{
    public partial class MainFormColor : Form
    {
        public MainFormColor()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var c = new ColorPicker();
            this.Hide();
            c.ShowDialog();
            this.Show();
            this.BackColor = c.SelectedColor;
        }

        private void btnPickColor_Click(object sender, EventArgs e)
        {
            //using (ColorPicker picker = new ColorPicker())
            //{
                ColorPicker picker = new ColorPicker();
                this.WindowState = FormWindowState.Minimized;
                picker.ShowDialog();

                for (int i = 7; i > 0; i--)
                {
                    ((Label)(this.Controls.Find("lblColor" + i.ToString(), true)[0])).BackColor = ((Label)(this.Controls.Find("lblColor" + (i - 1).ToString(), true)[0])).BackColor;
                }
                lblColor0.BackColor = picker.SelectedColor;
                toolShow_Click(null, null);
                lblColor_Click(lblColor0, null);
           // }

        }

        private void btnSelectColor_Click(object sender, EventArgs e)
        {
            colorDlg.Color = lblColor0.BackColor;

            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                lblColor0.BackColor = colorDlg.Color;
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("屏幕取色工具 V1.0\n", "关于 屏幕取色工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lblColor_Click(object sender, EventArgs e)
        {
            Label lbl = sender as Label;

            if (lbl != null)
            {
                Color color = lbl.BackColor;
                txtRGB.Text = string.Format("#{0,2:X2}{1,2:X2}{2,2:X2}", color.R.ToString("X"), color.G.ToString("X"), color.B.ToString("X")).Replace(" ", "0");
                txtARGB.Text = string.Format("{0,3:D3} {1,3:D3} {2,3:D3} {3,2:D3}", color.A, color.R, color.G, color.B);

                int hue = (int)((color.GetHue() / 360.0f) * 240 + 0.5);
                int sat = (int)(color.GetSaturation() * 241 + 0.5);
                int bri = (int)(color.GetBrightness() * 241 + 0.5);

                if (hue > 239)
                {
                    hue = 239;
                }

                if (sat > 240)
                {
                    sat = 240;
                }

                if (bri > 240)
                {
                    bri = 240;
                }

                txtHSB.Text = string.Format("{0,3:D3} {1,3:D3} {2,3:D3}", hue, sat, bri);
            }
        }

        private void lblColor_DoubleClick(object sender, EventArgs e)
        {
            Label lbl = sender as Label;

            if (lbl != null)
            {
                Color color = lbl.BackColor;
                colorDlg.Color = color;

                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    lbl.BackColor = colorDlg.Color;
                }
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void toolShow_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void ntyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            toolShow_Click(null, null);
        }
    }
}
